package com.medicine.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medicine.sys.entity.Department;
import com.medicine.sys.entity.Role;
import com.medicine.sys.entity.UserDepartment;
import com.medicine.sys.entity.UserRole;
import com.medicine.sys.mapper.UserRoleMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

@Service
public class UserRoleService extends ServiceImpl<UserRoleMapper, UserRole> {


    public void install(UserRole userRole) throws InvocationTargetException, IllegalAccessException {
        UserRole userRoleOne = getUserId(userRole.getUserId());
        if(userRoleOne!=null){
            userRoleOne.setRoleId(userRole.getRoleId());
        }else{
            userRoleOne = new UserRole();
            BeanUtils.copyProperties(userRoleOne,userRole);
        }
        saveOrUpdate(userRoleOne);
    }

    public UserRole getUserId(String userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return getOne(queryWrapper);
    }

    public Role getOneUserId(String userId){
        return this.baseMapper.getOneUserId(userId);
    }

}
